package com.jhws.business.feedback.service;

import com.alibaba.fastjson.JSON;
import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.feedback.bo.FeedbackInfo;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.common.base.bo.Global;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class FeedbackService {
    @Resource
    private BaseDao BaseDao;
    @Resource
    private ITblUserDimService IUserService;
    @Resource
    private OSSClientService ossClientService;

    /**
     * 保存反馈信息
     *
     * @param feedbackInfo 反馈信息
     * @return id
     */
    public String saveFeedback(FeedbackInfo feedbackInfo) {
        feedbackInfo.setCommitTime(new Date());
        BaseDao.h().save(feedbackInfo);
        return feedbackInfo.getId();
    }

    /**
     * 分页查询反馈信息
     *
     * @param param map
     * @return
     */
    public Map<String, Object> findFeedbackListPage(Map param) {
        String whereClause = "1=1 ";
        TblUserDimBo userBo = IUserService.currentUser();
        if (!SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN)) {
            whereClause += " and user = '" + userBo.getLogName() + "'";
        }

        String state = MapUtils.getString(param, "state", "");
        if (StringUtils.isNotEmpty(state)) {
            whereClause += " and state = '" + state + "'";
        }

        String sql = "select * from sys_feedback_info where " + whereClause;

        Map<String, Object> result = new HashMap<>();
        Page p = BaseDao.queryPage(sql, Map.class);
        List<Map<String, Object>> list = p.getList();

        for (Map<String, Object> map : list) {
            Object objUrl = map.get("urls");
            List<String> urls = objUrl == null ? new ArrayList<>() : JSON.parseArray(objUrl.toString(), String.class);
            List<String> ossUrls = new ArrayList<>();
            for (String url : urls) {
                ossUrls.add(ossClientService.getUrl(url, false));
            }
            map.replace("urls", ossUrls);
        }
        result.put("list", p.getList());
        result.put("size", p.getTotalNumber());
        result.put("ret_code", 1);
        return result;
    }

    /**
     * 条件查询
     *
     * @param where
     * @return
     */
    public List<FeedbackInfo> queryFeedbackInfoByWhere(String where) {
        return BaseDao.findList("from FeedbackInfo " + where);
    }

    /**
     * 获取反馈信息数量
     *
     * @param where
     * @return
     */
    public int findFeedbackCount(String where) {
        String sql = "select count(*) as sum from sys_feedback_info " + where;
        return BaseDao.j().queryForObject(sql, Integer.class);
    }

    public void updateFeedback(String set, String where) {
        String sql = "update sys_feedback_info " + set + where;
        BaseDao.j().execute(sql);
    }
}
